package com.horay.springbootdemo.springfox.swagger2.controller;

import com.horay.springbootdemo.springfox.swagger2.condition.Swagger2Condition;
import com.horay.springbootdemo.springfox.swagger2.controller.result.Responses;
import com.horay.springbootdemo.springfox.swagger2.controller.result.ResultBody;
import com.horay.springbootdemo.springfox.swagger2.dto.Swagger2DTO;
import com.horay.springbootdemo.springfox.swagger2.model.Swagger2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2演示控制器
 *
 * @author heyingcheng
 * @date 2020/2/16 17:50
 */
@RestController
@RequestMapping("springfox/swagger2")
@Api(value = "springfox/swagger2", tags = "Swagger2管理", description = "关于Swagger2的操作")
public class Swagger2Controller {

    @PostMapping
    @ApiOperation(value = "新增Swagger2", notes = "这只能由登录的用户完成")
    public ResponseEntity<ResultBody<String>> addSwagger2(@RequestBody @ApiParam(value = "新增的Swagger2数据传输对象", required = true) Swagger2DTO swagger2DTO) {
        return Responses.ok();
    }

    @PutMapping
    @ApiOperation(value = "更新Swagger2", notes = "这只能由登录的用户完成")
    public ResponseEntity<ResultBody<String>> updateSwagger2(@RequestBody @ApiParam(value = "更新的Swagger2数据传输对象", required = true) Swagger2DTO swagger2DTO) {
        return Responses.ok();
    }

    @GetMapping
    @ApiOperation(value = "查询Swagger2列表", notes = "这只能由登录的用户完成")
    public ResponseEntity<ResultBody<List<Swagger2>>> getSwagger2(@RequestBody @ApiParam(value = "查询Swagger2集合的条件", required = true) Swagger2Condition swagger2Condition) {
        List<Swagger2> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(new Swagger2(i, "name".concat(String.valueOf(i)), "description".concat(String.valueOf(i))));
        }
        return Responses.ok(list);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除Swagger2", notes = "这只能由登录的用户完成")
    public ResponseEntity<ResultBody<String>> deleteSwagger2(@PathVariable Integer id) {
        return Responses.unprocessableEntity("自定义错误");
    }

    @GetMapping("{id}")
    @ApiOperation(value = "按ID查询Swagger2", notes = "这只能由登录的用户完成")
    public ResponseEntity<ResultBody<Swagger2>> getSwagger2ById(@PathVariable Integer id) {
        return Responses.ok(new Swagger2(id, "name".concat(String.valueOf(id)), "description".concat(String.valueOf(id))));
    }

}
